package com.zz.nondatain.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewbinding.ViewBinding;

/**
 * 所有Fragment的基类，处理共有的逻辑
 * @param <T> ViewBinding 类型
 */
public abstract class BaseFragment<T extends ViewBinding> extends Fragment {
    
    protected T binding;
    
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = getViewBinding(inflater, container);
        return binding.getRoot();
    }
    
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        initData();
        initObservers();
    }
    
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
    
    /**
     * 初始化视图
     */
    protected abstract void initView();
    
    /**
     * 初始化数据
     */
    protected abstract void initData();
    
    /**
     * 初始化观察者
     */
    protected abstract void initObservers();
    
    /**
     * 获取ViewBinding实例
     */
    protected abstract T getViewBinding(LayoutInflater inflater, ViewGroup container);
    
    /**
     * 显示Toast消息
     */
    protected void showToast(String message) {
        Toast.makeText(requireContext(), message, Toast.LENGTH_SHORT).show();
    }
} 